<!DOCTYPE html>
<html lang="zh-CN">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  <link rel="icon" href="">
  <title>c++primary（下部） | ALgarth's blog</title>
  <meta name="author" content="ALgarth" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="c++从入门到不入门" />
  
  <meta name="description" content="真没想到一天就肝出来了，那先附上上一部的介绍  本篇博客为的是复习一遍C++的基础知识，方便从C语言更快的转型为C++语言 本文只是快速的提供模板和粗略的将部分代码一笔带过 前言中当然要感谢柳婼姐和CSDN的c++相关资料， 那么，我们进入正题：  C语言向C++的进阶本文打算按照以下顺序进行讲解  1.C++ 语言的优势 2.初步的C++用法介绍 3.板子的提出 4.依据板子进行其他的讲解 5.">
<meta property="og:type" content="article">
<meta property="og:title" content="c++primary（下部）">
<meta property="og:url" content="http://algarth.github.io/2020/10/11/c-primary-1/index.html">
<meta property="og:site_name" content="ALgarth&#39;s blog">
<meta property="og:description" content="真没想到一天就肝出来了，那先附上上一部的介绍  本篇博客为的是复习一遍C++的基础知识，方便从C语言更快的转型为C++语言 本文只是快速的提供模板和粗略的将部分代码一笔带过 前言中当然要感谢柳婼姐和CSDN的c++相关资料， 那么，我们进入正题：  C语言向C++的进阶本文打算按照以下顺序进行讲解  1.C++ 语言的优势 2.初步的C++用法介绍 3.板子的提出 4.依据板子进行其他的讲解 5.">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://algarth.github.io/null">
<meta property="article:published_time" content="2020-10-11T08:15:40.000Z">
<meta property="article:modified_time" content="2020-10-31T02:46:07.304Z">
<meta property="article:author" content="ALgarth">
<meta property="article:tag" content="c++从入门到不入门">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://algarth.github.io/null">
<meta name="twitter:site" content="@null">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/css/kratosr.min.css" type="text/css" media="all">
  <link rel="stylesheet" id="highlight-css" href="/css/highlight.min.css" type="text/css" media="all">
  <link rel="stylesheet" id="fontawe-css" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" type="text/css" media="all">
  <link rel="stylesheet" id="nprogress-css" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" type="text/css" media="all">
  
  
  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
  
  
    <link rel="stylesheet" id="darkmode-css" href="/css/kr-dark.min.css" type="text/css" media="all">
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
  
    <script src="https://cdn.jsdelivr.net/npm/qrcode_js@1.0.0/qrcode.min.js"></script>
  
  
<meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/atom.xml" title="ALgarth's blog" type="application/atom+xml">
</head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <div class="nav-toggle"><a class="kratos-nav-toggle js-kratos-nav-toggle"><i></i></a></div>
            <header id="kratos-header-section">
                <div class="container">
                    <div class="nav-header">
                        <div class="color-logo"><a href="/">ALgarth&#39;s blog</a></div>
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                        <li><a href="/"><i class="fa fa-home"></i>首页</a></li>
                                    
                                
                                    
                                        <li><a href="/archives/"><i class="fa fa-file"></i>档案馆</a></li>
                                    
                                
                                    
                                        <li>
                                            <a><i class="fa fa-link"></i>链接</a>
                                            <ul class="sub-menu">
                                                
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://blog.csdn.net/algarth">作者博客</a></li>
                                                    
                                                
                                                    
                                                        <li><a target="_blank" rel="noopener" href="https://github.com/ALgarth">项目链接</a></li>
                                                    
                                                
                                            </ul>
                                        </li>
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/">
                        <h2>ALgarth&#39;s blog</h2> <br />
                        <span>欢迎捞到摸鱼仙人的博客呢_(:3」∠)_</span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div class="row">
                    
        <div id="main">
            <section class="col-md-8">
    <article>
        <div class="kratos-hentry kratos-post-inner clearfix">
            <header class="kratos-entry-header">
                <h1 class="kratos-entry-title text-center">c++primary（下部）</h1>
                
                <div class="kratos-post-meta text-center">
                    <span>
                        <i class="fa fa-calendar"></i> 2020-10-11
                        <i class="fa fa-folder"></i> 分类于 
                        <i class="fa fa-user"></i> 作者 ALgarth
                        <i class="fa fa-edit"></i> 
                        
                        
                            ~5.07K
                        
                        字
                        
                    </span>
                </div>
            </header>
            <div class="kratos-post-content">
                
                    <div class="kratos-post-inner-toc">
                        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#C%E8%AF%AD%E8%A8%80%E5%90%91C-%E7%9A%84%E8%BF%9B%E9%98%B6"><span class="toc-number">1.</span> <span class="toc-text">C语言向C++的进阶</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B-%E4%BE%9D%E6%8D%AE%E6%9D%BF%E5%AD%90%E8%BF%9B%E8%A1%8C%E5%85%B6%E4%BB%96%E7%9A%84%E8%AE%B2%E8%A7%A3"><span class="toc-number">1.1.</span> <span class="toc-text">四. 依据板子进行其他的讲解</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#string"><span class="toc-number">1.1.1.</span> <span class="toc-text">string</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#vector"><span class="toc-number">1.1.2.</span> <span class="toc-text">vector</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#set"><span class="toc-number">1.1.3.</span> <span class="toc-text">set</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map"><span class="toc-number">1.1.4.</span> <span class="toc-text">map</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94-%E6%9D%BF%E5%AD%90%E4%B8%AD%E5%85%B6%E4%BB%96%E5%A4%B4%E6%96%87%E4%BB%B6%E7%9A%84%E7%94%A8%E6%B3%95%E8%AF%B4%E6%98%8E"><span class="toc-number">1.2.</span> <span class="toc-text">五. 板子中其他头文件的用法说明</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD-%E5%85%B6%E4%BB%96%E4%B8%8D%E5%9C%A8%E6%9D%BF%E5%AD%90%E5%86%85%E7%9A%84%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%86%99%E6%B3%95%E5%92%8CC-11"><span class="toc-number">1.3.</span> <span class="toc-text">六.其他不在板子内的常用函数写法和C++11</span></a></li></ol></li></ol>
                    </div>
                
                <hr />
                <p>真没想到一天就肝出来了，那先附上上一部的介绍</p>
<blockquote>
<p>本篇博客为的是复习一遍C++的基础知识，方便从C语言更快的转型为C++语言</p>
<p>本文只是快速的提供模板和粗略的将部分代码一笔带过</p>
<p>前言中当然要感谢柳婼姐和CSDN的c++相关资料，</p>
<p>那么，我们进入正题：</p>
</blockquote>
<h1 id="C语言向C-的进阶"><a href="#C语言向C-的进阶" class="headerlink" title="C语言向C++的进阶"></a>C语言向C++的进阶</h1><p>本文打算按照以下顺序进行讲解</p>
<blockquote>
<p>1.C++ 语言的优势</p>
<p>2.初步的C++用法介绍</p>
<p>3.板子的提出</p>
<p>4.依据板子进行其他的讲解</p>
<p>5.板子中其他头文件的用法说明</p>
<p>6.其他不在板子内的常用函数写法和C++11</p>
</blockquote>
<p><strong>书接上文</strong></p>
<h2 id="四-依据板子进行其他的讲解"><a href="#四-依据板子进行其他的讲解" class="headerlink" title="四. 依据板子进行其他的讲解"></a>四. 依据板子进行其他的讲解</h2><p>这个其他的讲解，主要就是讲解内部几个好用的头文件的使用方法</p>
<h3 id="string"><a href="#string" class="headerlink" title="string"></a>string</h3><p>string我觉得是c++的至关重要的元素，就是有了这个最好用的string类，才能劝很多C语言小同学入坑~</p>
<p>使用样例如下</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt; //为了下面的reverse加的www</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="built_in">string</span> a&#123;<span class="string">&quot;Algarth!!&quot;</span>&#125;;<span class="comment">//c++提供的大括号初始化</span></span><br><span class="line">	<span class="built_in">cout</span>&lt;&lt;a&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">string</span> b=<span class="string">&quot;123&quot;</span>;</span><br><span class="line">	<span class="built_in">cout</span>&lt;&lt;a+b&lt;&lt;<span class="built_in">endl</span>; <span class="comment">//string可以进行加法运算</span></span><br><span class="line">	<span class="keyword">int</span> num=stoi(b); <span class="comment">//c++11新函数，可以直接string_to_int</span></span><br><span class="line">	<span class="built_in">cout</span>&lt;&lt;num&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">	<span class="built_in">cout</span>&lt;&lt;<span class="built_in">string</span>(a.rbegin(),a.rend()); <span class="comment">//应用迭代器进行倒叙输出a</span></span><br><span class="line">	reverse(a.begin(),a.end());<span class="comment">//应用algorithm头文件进行翻转a</span></span><br><span class="line">    <span class="built_in">cout</span>&lt;&lt;a.length(); <span class="comment">//求a的长度，一般用在for循环中比较好用</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s&quot;</span>,a.c_str()); <span class="comment">//string只能使用cin，getline，cout之类的输入输出，不能使用scanf和printf，但是可以用这种方法实现c++转c格式进行printf</span></span><br><span class="line">    <span class="built_in">cout</span>&lt;&lt;s.substr(<span class="number">1</span>,<span class="number">2</span>);<span class="comment">//从下标1取长度为2的子串</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里我直接罗列了一般常用的方法，细节写在上面了。</p>
<p>上文没提到的比如：</p>
<p>这个string头文件和cstring不一样，cstring是当时string.h那个，但是现在有了string用不到里面的函数了，而且这个string头文件很多部分在dev编译器中包含在了std中，所以其实也可以不写，<del>据说VS不行</del></p>
<h3 id="vector"><a href="#vector" class="headerlink" title="vector"></a>vector</h3><p>最好使的动态数组</p>
<p>你还在为非动态数组越界烦恼吗？？</p>
<p>你还在为动态数组使用malloc和calloc困扰吗？？</p>
<p>甚至是在为数组不能用a[n]不爽吗？？</p>
<p>欢迎来到最爽的vector</p>
<p>首先声明一下内容方便理解：</p>
<p>vector和后面的set，map之类的都是容器，容器是STL即标准模板库，标准模板库使用的时候应用了C++的template用法，这种template用法使用的时候，必须先指定套用的模板类型，可以理解为，模板是不指定类型的函数，手动指定函数之后，系统自动生成一套函数用来调用，而且C++的重载使得一个函数可以有不同实现方法，所以可以生成不同的函数。</p>
<p>给出下文的应用方法</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt; </span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; v1&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;; <span class="comment">//一种初始化的方法 </span></span><br><span class="line">	<span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">double</span>&gt; <span class="title">v2</span><span class="params">(<span class="number">10</span>)</span></span>;  <span class="comment">//创建有一定空间的vector</span></span><br><span class="line">	<span class="function"><span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; <span class="title">v3</span><span class="params">(<span class="number">10</span>,<span class="number">0</span>)</span></span>;   <span class="comment">//创建一个有一定空间的vector，并全部初始化为0</span></span><br><span class="line">	<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; v4;   <span class="comment">//创建一个有0个空间的vector</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;v3.size();++i)&#123; <span class="comment">//进行正常的访问，.size获得长度</span></span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;v3[i]&lt;&lt;<span class="string">&quot; &quot;</span>;</span><br><span class="line">	&#125; </span><br><span class="line">	<span class="keyword">for</span>(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;::iterator it=v3.begin();v3!=v3.end();++it)&#123;<span class="comment">//迭代器进行访问</span></span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;*it&lt;&lt;<span class="string">&quot; &quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">    v4.push_back(<span class="number">1</span>); <span class="comment">//在后面推入1</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>迭代器是C++中很关键的一环，C++中将容器和迭代器分开存放，使得不会互相干扰，其实直接理解为迭代器是指针的增强版就行，用于进行访问，后面的很多容器不能使用int i的方式进行遍历，比如list链表就不可以随机访问对吧，所以只能使用迭代器，迭代器的妙用可以从下面的一个例子看出</p>
<p>比如在vector线性表和list链表中使用迭代器it，同时进行it++,都是去访问下一个，对于数组而言it就是向后移动一个单位，可是链表肯定不是单纯的地址++，而是在迭代器中重载了迭代器的++操作</p>
<p>在使用迭代器的时候，可以直接使用c++11的auto，auto自动确定类型，这个后面再说</p>
<p>push_back也是很好用的手法，推入一个元素，当然c++11有emplace_back()，用于减少一次拷贝构造</p>
<h3 id="set"><a href="#set" class="headerlink" title="set"></a>set</h3><p>set相当于一个集合，内部元素具有互斥性，insert相同元素的时候不会影响.size，因此set一个很好的用法就是统计重复数字。而且会对插入的元素进行排序，内部使用平衡二叉树实现，所以算法的时间复杂度不高，而且像vector一样可以指定排序方法，这个先不讲。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;set&gt; </span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="built_in">set</span>&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">	s.insert(<span class="number">1</span>); <span class="comment">//插入1</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">auto</span> it=s.begin();it!=s.end();++it)&#123;</span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;*it&lt;&lt;<span class="string">&quot; &quot;</span>;<span class="comment">//进行访问值</span></span><br><span class="line">	&#125; </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="map"><a href="#map" class="headerlink" title="map"></a>map</h3><p>map是一个映射，map要求指定前键和后键，也就是.first和.second。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;map&gt; </span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="built_in">map</span>&lt;<span class="keyword">int</span>,<span class="built_in">string</span>&gt; m;</span><br><span class="line">	m[<span class="number">1</span>]=<span class="string">&quot;ALgarth&quot;</span>;</span><br><span class="line">	<span class="built_in">map</span>&lt;<span class="built_in">string</span>,<span class="keyword">int</span>&gt; m1;</span><br><span class="line">	m1[<span class="string">&quot;ALgarth&quot;</span>]=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">auto</span> it=m.begin();it!=m.end();++it)&#123;</span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;(*it).first&lt;&lt;<span class="string">&quot; &quot;</span>&lt;&lt;(*it).second&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">		<span class="built_in">cout</span>&lt;&lt;it-&gt;first&lt;&lt;<span class="string">&quot; &quot;</span>&lt;&lt;it-&gt;second&lt;&lt;<span class="built_in">endl</span>; </span><br><span class="line"><span class="comment">//		上述两种方法进行遍历都可以</span></span><br><span class="line"><span class="comment">//		first是前键，second后键，顺序根据定义中给的那个一样 </span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span>&lt;&lt;m.size(); </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当然剩下的篇章应该还有queue，stack，unordered_set，unordered_map，bitset</p>
<p><del>（这里其实省略了几个重要内容，诸如vector二维数组的使用，map如何使用2对1的映射，这些希望各位自己去了解）</del></p>
<p>希望更多的STL内容希望由各位自己去发现</p>
<h2 id="五-板子中其他头文件的用法说明"><a href="#五-板子中其他头文件的用法说明" class="headerlink" title="五. 板子中其他头文件的用法说明"></a>五. 板子中其他头文件的用法说明</h2><p>对这就是说的是algorithm头文件</p>
<p>这个提供了sort函数的头文件我称之为神不过分吧？？</p>
<p>他集合了C语言一辈子都没有的max，min函数。同时提供sort函数，reverse函数，二分查找的lower_bound和upper_bound。</p>
<p>本次就先简单介绍一下sort函数大法</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Ag</span> &#123;</span></span><br><span class="line">	<span class="keyword">int</span> name;</span><br><span class="line">	<span class="keyword">int</span> id;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(Ag a,Ag b)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(a.name!=b.name) <span class="keyword">return</span> a.name&lt;b.name;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">return</span> a.id&lt;b.id;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//这是一种写自定义排序的方法，写cmp函数 </span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Ag1</span> &#123;</span></span><br><span class="line">	<span class="keyword">int</span> name;</span><br><span class="line">	<span class="keyword">int</span> id;</span><br><span class="line">	<span class="keyword">bool</span> <span class="keyword">operator</span> &lt; (Ag1 b) &#123;</span><br><span class="line">		<span class="keyword">if</span>(name!=b.name) <span class="keyword">return</span> name&lt;b.name;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">return</span> id&lt;b.id;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//这是另一种，直接重载运算符 </span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; v &#123;<span class="number">2</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">3</span>&#125;;</span><br><span class="line">	sort(v.begin(),v.end());<span class="comment">//传的这两个参数为头指针和尾指针 </span></span><br><span class="line">	<span class="keyword">int</span> a[]=&#123;<span class="number">2</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>&#125;;</span><br><span class="line">	sort(a,a+<span class="number">4</span>);<span class="comment">//传的两个参数也刚好为两个指针 </span></span><br><span class="line">	<span class="built_in">vector</span>&lt;Ag&gt; aaa&#123;&#123;<span class="number">1</span>,<span class="number">2</span>&#125;,&#123;<span class="number">3</span>,<span class="number">4</span>&#125;&#125;;<span class="comment">//用C++进行初始化一个size为2的结构体组 </span></span><br><span class="line">	<span class="built_in">vector</span>&lt;Ag1&gt; aaa1&#123;&#123;<span class="number">1</span>,<span class="number">2</span>&#125;,&#123;<span class="number">3</span>,<span class="number">4</span>&#125;&#125;;</span><br><span class="line">	sort(aaa.begin(),aaa.end(),cmp);</span><br><span class="line">	sort(aaa1.begin(),aaa1.end()); </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="六-其他不在板子内的常用函数写法和C-11"><a href="#六-其他不在板子内的常用函数写法和C-11" class="headerlink" title="六.其他不在板子内的常用函数写法和C++11"></a>六.其他不在板子内的常用函数写法和C++11</h2><p>C++现在推出到14版了，但是其实很多地方使用的时候都没用到老一点的11版，11年加入了很多非常好用的C++使用方法，比如stoi，to_string，unordered_map等，使用devc编译的话记得在编译环境里加上-std=c++11，具体操作可以百度</p>
<p>多说一点，很多平台已经可以使用C++11了，但是有些比赛上对于C++11是禁用的，比如蓝桥杯<del>（在前几个填空题的时候自已用用）</del>，理由很有意思，是老码农不会，不能加重老码农的学习负担，<del>实在是不能卷啊</del></p>
<p>其他不在板子中的函数比如这个to_string()函数，这个函数可以直接将中间的值比如int型转换为string型，非常方便。</p>
<p>剩下的还有几个函数比如stoi（string转换为int），stod（double），stoll（long long）等等</p>
<p>而且迭代器一般要写很长，现在直接写auto自动判断就行，爽啊</p>
<p>内容到这里就结束了，希望各位有所收获</p>
<hr>
<p><em>我是ALgarth，如果有缘，我们下次再见。</em></p>
<p><em>图片来自网络，侵删。</em></p>
<p><em>本人拙笔草见，如果内容有误，请及时联系修改</em></p>

            </div>
            
                <div class="kratos-copyright text-center clearfix">
                    <h5>本作品采用 <a rel="license nofollow" target="_blank" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
                </div>
            
            <footer class="kratos-entry-footer clearfix">
                
                    <div class="post-like-donate text-center clearfix" id="post-like-donate">
                    
                        <a class="donate" href="javascript:;"><i class="fa fa-bitcoin"></i> 打赏</a>
                    
                    
                        <a class="share" href="javascript:;"><i class="fa fa-share-alt"></i> 分享</a>
                        <div class="share-wrap" style="display: none;">
    <div class="share-group">
        <a href="javascript:;" class="share-plain qq" onclick="share('qq');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-qq"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain qzone" onclick="share('qzone');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-star"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weixin pop style-plain" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weixin"></i>
            </div>
            <div class="share-int">
                <div class="qrcode" id="wechat-qr"></div>
                <p>打开微信“扫一扫”，打开网页后点击屏幕右上角分享按钮</p>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weibo" onclick="share('weibo');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weibo"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain facebook style-plain" onclick="share('facebook');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-facebook"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain twitter style-plain" onclick="share('twitter');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-twitter"></i>
            </div>
        </a>
    </div>
    <script type="text/javascript">
        $(()=>{
            new QRCode("wechat-qr", {
                text: "http://algarth.github.io/2020/10/11/c-primary-1/",
                width: 150,
                height: 150,
                correctLevel : QRCode.CorrectLevel.H
            });
        });
        function share(dest) {
            const qqBase        = "https://connect.qq.com/widget/shareqq/index.html?";
            const weiboBase     = "https://service.weibo.com/share/share.php?";
            const qzoneBase     = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
            const facebookBase  = "https://www.facebook.com/sharer/sharer.php?";
            const twitterBase   = "https://twitter.com/intent/tweet?";
            const hostUrl       = "http://algarth.github.io/2020/10/11/c-primary-1/";
            const title         = "「c++primary（下部）」";
            const excerpt       = `真没想到一天就肝出来了，那先附上上一部的介绍

本篇博客为的是复习一遍C++的基础知识，方便从C语言更快的转型为C++语言
本文只是快速的提供模板和粗略的将部分代码一笔带过
前言中当然要感谢柳婼姐和CSDN的c++相关资料，
那么，我...`;
            let _URL;
            switch (dest) {
                case "qq"       : _URL = qqBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";     break;
                case "weibo"    : _URL = weiboBase+"url="+hostUrl+"&title="+title+excerpt;                                 break;
                case "qzone"    : _URL = qzoneBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";  break;
                case "facebook" : _URL = facebookBase+"u="+hostUrl;                                                        break;
                case "twitter"  : _URL = twitterBase+"text="+title+excerpt+"&url="+hostUrl;                                break;
            }
            window.open(_URL);
        };
    </script>
</div>
                    
                    </div>
                
                <div class="footer-tag clearfix">
                    <div class="pull-left">
                    <i class="fa fa-tags"></i>
                        <a class="tag-none-link" href="/tags/c-%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E4%B8%8D%E5%85%A5%E9%97%A8/" rel="tag">c++从入门到不入门</a>
                    </div>
                    <div class="pull-date">
                    <span>最后编辑：2020-10-31</span>
                    </div>
                </div>
            </footer>
        </div>
        
            <nav class="navigation post-navigation clearfix" role="navigation">
                
                <div class="nav-previous clearfix">
                    <a title=" C++ primary（上部）" href="/2020/10/11/C-primary/">&lt; 上一篇</a>
                </div>
                
                
                <div class="nav-next clearfix">
                    <a title=" priority" href="/2020/10/12/priority/">下一篇 &gt;</a>
                </div>
                
            </nav>
        
        
    </article>
</section>
        </div>
                
            <section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
                
                    <aside id="krw-about" class="widget widget-kratos-about clearfix">
    
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="/images/avatar.webp" />
        </div>
    </div>
    <div class="textwidget">
        
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#C%E8%AF%AD%E8%A8%80%E5%90%91C-%E7%9A%84%E8%BF%9B%E9%98%B6"><span class="toc-number">1.</span> <span class="toc-text">C语言向C++的进阶</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B-%E4%BE%9D%E6%8D%AE%E6%9D%BF%E5%AD%90%E8%BF%9B%E8%A1%8C%E5%85%B6%E4%BB%96%E7%9A%84%E8%AE%B2%E8%A7%A3"><span class="toc-number">1.1.</span> <span class="toc-text">四. 依据板子进行其他的讲解</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#string"><span class="toc-number">1.1.1.</span> <span class="toc-text">string</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#vector"><span class="toc-number">1.1.2.</span> <span class="toc-text">vector</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#set"><span class="toc-number">1.1.3.</span> <span class="toc-text">set</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map"><span class="toc-number">1.1.4.</span> <span class="toc-text">map</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94-%E6%9D%BF%E5%AD%90%E4%B8%AD%E5%85%B6%E4%BB%96%E5%A4%B4%E6%96%87%E4%BB%B6%E7%9A%84%E7%94%A8%E6%B3%95%E8%AF%B4%E6%98%8E"><span class="toc-number">1.2.</span> <span class="toc-text">五. 板子中其他头文件的用法说明</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD-%E5%85%B6%E4%BB%96%E4%B8%8D%E5%9C%A8%E6%9D%BF%E5%AD%90%E5%86%85%E7%9A%84%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%86%99%E6%B3%95%E5%92%8CC-11"><span class="toc-number">1.3.</span> <span class="toc-text">六.其他不在板子内的常用函数写法和C++11</span></a></li></ol></li></ol>
        
    </div>
</aside>
                
                    <!-- Moved to about.ejs -->
                
                    

                
                    
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/tags/OS/" style="font-size: 0.6em;">OS</a> <a href="/tags/c-%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E4%B8%8D%E5%85%A5%E9%97%A8/" style="font-size: 0.7em;">c++从入门到不入门</a> <a href="/tags/priority-queue/" style="font-size: 0.6em;">priority_queue</a> <a href="/tags/projectpool/" style="font-size: 0.6em;">projectpool</a> <a href="/tags/tmp/" style="font-size: 0.6em;">tmp</a> <a href="/tags/trailer/" style="font-size: 0.7em;">trailer</a> <a href="/tags/update/" style="font-size: 0.6em;">update</a> <a href="/tags/%E5%89%8D%E8%A8%80/" style="font-size: 0.6em;">前言</a> <a href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/" style="font-size: 0.8em;">计算机组成原理</a> <a href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86%E6%B1%87%E6%80%BB/" style="font-size: 0.6em;">计算机组成原理汇总</a> <a href="/tags/%E8%AE%A1%E7%BD%91/" style="font-size: 0.6em;">计网</a>
      </div>
  </aside>

                
                    
  <aside id="krw-posts" class="widget widget-kratos-poststab">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/2021/12/23/tmp/"><i class="fa  fa-book"></i> 小范围开源期末材料</a>
            
          
        
          
          
            <a class="list-group-item" href="/2021/10/30/OS/"><i class="fa  fa-book"></i> OS第一章</a>
            
          
        
          
          
            <a class="list-group-item" href="/2021/10/23/cnet/"><i class="fa  fa-book"></i> 计网第一章</a>
            
          
        
          
          
        
          
          
            <a class="list-group-item" href="/2021/10/19/projectpool/"><i class="fa  fa-book"></i> 计划进程池设定</a>
            
          
        
          
          
        
          
          
            <a class="list-group-item" href="/2020/12/31/organization-ex/"><i class="fa  fa-book"></i> 计算机组成原理汇总篇</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

                
            </section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer">
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        
                        <li><a href="mailto:algarth@163.com"><i class="fa fa-envelope"></i></a></li>
                        
                        
                        
                        
                        
                        <li><a target="_blank" rel="nofollow" href="https://github.com/ALgarth"><i class="fa fa-github"></i></a></li>
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2022 ALgarth 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Made with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by <a href="https://candinya.com" target="_blank" rel="nofollow">Candinya</a>.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://github.io" target="_blank">Github Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>
<script>const notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));</script>

    <div>
        <canvas id="snow"></canvas>
        <script async type="text/javascript" src="/js/snow.min.js"></script>
    </div>

<script async src="/js/candy.min.js"></script>


    <script defer src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script>
<script defer src="/js/kratosr.min.js"></script>
<script defer src="/js/pjax.min.js"></script>

    <script defer src="https://cdn.jsdelivr.net/npm/layui-src@2.5.5/dist/layui.all.js"></script>


    <script defer src="/js/kr-dark.min.js"></script>


    </body>
</html>